//
//  RequestLoadingView.m
//  myhuibao
//
//  Created by ewenlong on 14-4-2.
//  Copyright (c) 2014年 myhuibao. All rights reserved.
//

#import "RequestLoadingView.h"
#import <QuartzCore/QuartzCore.h>

@implementation RequestLoadingView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


- (UIView *)initRequestLoadingView:(id)wdelegate queueFinishSelector:(SEL)sel viewFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        delegate = wdelegate ;
        queueFinishSelector = sel;
        
        self.backgroundColor = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 1];

        if(!_networkqueue)
        {
            _networkqueue = [[ASINetworkQueue alloc] init];
        }
        [_networkqueue setDelegate:self];
        
        [_networkqueue cancelAllOperations];
        
        [_networkqueue setQueueDidFinishSelector:@selector(DidqueueFinishSelector)];
        
        _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [_activityView setCenter:CGPointMake(self.frame.size.width/2,
                                             self.frame.size.height/2 - 40)];
        [_activityView setColor:[UIColor redColor]];
        
        CGRect activityViewFrame = _activityView.frame ;
        CGRect rc = CGRectMake(0, activityViewFrame.origin.y+activityViewFrame.size.height,self.frame.size.width, 40);
        UILabel *label = [[UILabel alloc] initWithFrame:rc];
        [label setTextAlignment:UITextAlignmentCenter];
        [label setTextColor:[UIColor redColor]];
        [label setFont:[UIFont systemFontOfSize:16]];
        label.text = @"加载中...";
        
        [self addSubview:label];
        
        [self addSubview:_activityView];
        
        [self startRequestActivityView];
        
        [[(UIViewController*)delegate view] addSubview:self];
        
        _bFirstRequest = YES ;
        
    }
    
    return self;
    
}

- (void)startRequestActivityView
{
    [_activityView startAnimating];
}

- (void)setActivityViewColor:(UIColor*)color
{
    [_activityView setColor:color];
}

- (void)setQueueFinishSelector:(SEL)sel
{
    queueFinishSelector = sel ;
}

//this method will be called when (queue.count == 0)
- (void)DidqueueFinishSelector
{
    [_activityView stopAnimating];
    
    if(delegate && [delegate respondsToSelector:queueFinishSelector])
    {
        [delegate performSelector:queueFinishSelector withObject:nil];
    }
    //self.hidden = YES ;
    _bFirstRequest = NO ;
    [self removeFromSuperview];
    
}

- (void)addRequestToQueue:(ASIHTTPRequest *)request
{
    if(_networkqueue && request)
    {
        [_networkqueue addOperation:request];
    }
}

- (void)setRequestFinishSelector:(SEL)sel
{
    [_networkqueue setRequestDidFinishSelector:sel];
}
- (void)setRequestFailSelector:(SEL)sel
{
    [_networkqueue setRequestDidFailSelector:sel];
}

- (void)startRequestQueue
{
    if(_networkqueue && [_networkqueue requestsCount] != 0)
    {
        [_networkqueue go];
    }
    else
    {
        NSLog(@"startRequestqueue failed");
    }
}

- (void)dealloc
{
    if([_networkqueue requestsCount] != 0)
    {
        [_networkqueue cancelAllOperations];
    }
    _bFirstRequest = NO ;
    [self setNetworkqueue:nil];
    [self setActivityView:nil];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
